package com.hula.onion.book.server.spider.platforms.darendai;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hula.onion.book.server.common.CommonResponse;
import com.hula.onion.book.server.common.LoginForm;
import com.hula.onion.book.server.config.ResponseStatusEnum;
import com.hula.onion.book.server.model.AccountInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * Created by work on 2018/2/12.
 */
@Component
public class DaRenDaiSpiderHelper {
    private static final Logger logger = LoggerFactory.getLogger(DaRenDaiSpiderHelper.class);

    public JSONObject installRequestParams(LoginForm loginForm) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("account", loginForm.getUserName());
        jsonObject.put("password", loginForm.getPassword());
        return jsonObject;
    }

    /**
     * 解析账户余额
     *
     * @param json
     * @return
     */


    /*****************************
     请求账户返回json
     {
     "account": {
     "balance": 0,
     "blockingCapital": 0,
     "createDate": 1518438408000,
     "creditorBalance": 0,
     "creditorInvestBlocking": 0,
     "creditorSquareBlocking": 0,
     "dcoin": 0,
     "id": 148002,
     "kftUsingBalance": 0,
     "repeatInvestAmount": 0,
     "sinaUsingBalance": 0,
     "usingBalance": 0
     },
     "allPayMoney": 0,
     "blance": 0,
     "currMonthPayMoney": 0,
     "currYearPayMoney": 0,
     "dcoin": 0,
     "isKftBalances": 0,
     "isOpenSinaWithhold": 0,
     "isZtbClear": 0,
     "nextMonthPayMoney": 0,
     "onlineUser": {
     "account": "15510252326",
     "accountStatus": 1,
     "createDate": 1518438422000,
     "deCrypt": true,
     "email": "",
     "id": 148002,
     "lastLoginTime": 1518438512174,
     "loginFailCount": 0,
     "loginIp": "61.148.243.188",
     "preLoginIp": "61.148.243.188",
     "preLoginTime": 1518438408000,
     "registerIp": "61.148.243.188",
     "riskAssess": false,
     "telPhone": "155******26"
     },
     "payInterest": 0,
     "payPrincipal": 0,
     "shareableRedCount": 0,
     "totalCapital": 0,
     "totalCashVoucherTicket": 0,
     "totalInterest": 0,
     "totalInvestment": 0,
     "totalPiggyBankIncome": 0,
     "totalPrivilegeAmount": 0,
     "usingBlance": 0,
     "yestodayPiggyBankIncome": 0
     }
     ***************************/
    public CommonResponse parseAccountBalance(String json) {
        if (StringUtils.isEmpty(json)) {
            return new CommonResponse(ResponseStatusEnum.QUERY_FAIL);
        }
        try {
            AccountInfo accountInfo = new AccountInfo();
            JSONObject jsonObject = JSON.parseObject(json);
            accountInfo.setAmount(jsonObject.getBigDecimal("totalCapital"));
            accountInfo.setTotalIntrest(jsonObject.getBigDecimal("totalInterest"));
            accountInfo.setWaitIntrest(jsonObject.getBigDecimal("payInterest"));
            accountInfo.setYesterdayIntrest(jsonObject.getBigDecimal("yestodayPiggyBankIncome"));
            return new CommonResponse(accountInfo);
        } catch (Exception e) {
            logger.error("[DaRenDaiSpiderHelper] parseAccountBalance fail!", e);
        }
        return new CommonResponse(ResponseStatusEnum.QUERY_FAIL);
    }
}
